﻿using UnityEngine;
using Swift;
using UniLua;

// 对应一个 lua 逻辑层的卡牌
public class LuaCard : LuaTable
{
    LuaTable tb;

    public LuaCard(LuaState ls, LuaTable c)
        : base(ls)
    {
        tb = c;
    }

    public string Name
    {
        get
        {
            return tb.GetStr("name").V.SValue();
        }
    }

    public string Desc
    {
        get
        {
            return tb.GetStr("desc").V.SValue();
        }
    }

    public int Cost
    {
        get
        {
            return (int)tb.GetStr("cost").V.NValue;
        }
    }

    public string Type
    {
        get
        {
            return tb.GetStr("type").V.SValue();
        }
    }

    public bool CanUse(LuaMonster m)
    {
        var ls = CombatMgr.ls;
        ls.PushTable(tb);
        ls.GetField(-1, "canUse");
        ls.PushValue(-2);
        ls.PushTable(m == null ? null : m.LuaTable);
        var state = ls.PCall(2, 1, CombatMgr.b);
        var r = false;
        if (state == ThreadStatus.LUA_OK)
            r = ls.ToBoolean(-1);
        else
            UniLua.Tools.ULDebug.LogError(ls.L_ToString(-1));

        ls.Pop(2);
        return r;
    }

    public string Pile { get; set; }
    public void Use(LuaMonster m)
    {
        var ls = CombatMgr.ls;
        ls.PushTable(CombatMgr.hCbt);
        ls.GetField(-1, "useCard");
        ls.PushValue(-2);
        ls.PushString(Pile);
        ls.PushTable(tb);
        ls.PushTable(m == null ? null : m.LuaTable);
        if (ls.PCall(4, 0, CombatMgr.b) != ThreadStatus.LUA_OK)
            UniLua.Tools.ULDebug.LogError(ls.L_ToString(-1));

        ls.Pop(1);
    }
}